<?php
/**
 * Created by 荣耀电竞.
 * User: 林子彦 <278805354@qq.com>
 * Date: 2018/7/10 0010
 * Time: 17:12
 */

namespace App\Ordersys\Models;

use App\Base\Models\BaseModel;
use App\Ordersys\Exceptions\OrdersysException;

class OrdersysScoreLogModel extends BaseModel
{
    /**
     * 表名
     * @var string
     */
    public $table = "ordersys_score_log";
    /**
     * 时间自动
     * @var bool
     */
    public $timestamps = true;
    /**
     * The name of the "created at" column.
     *
     * @var string
     */
    const CREATED_AT = 'create_time';  //修改添加时间字段
    /**
     * The name of the "updated at" column.
     *
     * @var string
     */
    const UPDATED_AT = 'update_time';

    /**
     * 删除
     */
    const DELETED_AT = 'delete_time';
    /**
     * 模型的日期字段保存格式。
     */
    protected $dateFormat = 'U';


    /**
     * vip用户模型
     * @return OrdersysCustomerModel
     */
    public function getOrdersysCustomerModel()
    {
        return app()->make(OrdersysCustomerModel::class);
    }

    /**
     * 金额操作
     * @param $type
     * @param array $data
     * @throws OrdersysException
     */
    public function balanceLog($type, $data = array())
    {
        $amount = (float)$data['amount'];
        if ($amount < .01) {
            throw new OrdersysException(OrdersysException::EROOR_BALANCE_AMOUNT_NOT);
        }
        if ($amount == '0.00' || empty($amount) || $data['amount'] == 0.00) {
            throw new OrdersysException(OrdersysException::EROOR_BALANCE_AMOUNT_NOT);
        }
        switch ($type) {
            case 'backscore':
                $available = +$amount;
                $desc = '销售员[' . $data['sale_name'] . ']返回的积分:' . $amount . '，订单号: ' . $data['order_id'];
                $this->getOrdersysCustomerModel()->newQuery()->where("id",$data["member_id"])->increment("score",$amount);
                break;
            case 'system_recharge':  //系统充值
                $available = +$amount;
                $desc = '操作员[' . $data['apply_name'] . '],系统充值积分:' . $amount ;
                $this->getOrdersysCustomerModel()->newQuery()->where("id",$data["member_id"])->increment("score",$amount);
                break;
            case 'system_deduction':  //系统扣款
                $available = -$amount;
                $desc = '操作员[' . $data['apply_name'] . '],系统扣除积分:' . $amount;
                $this->getOrdersysCustomerModel()->newQuery()->where("id",$data["member_id"])->decrement("score",$amount);
                break;
            case 'consume':  //消费
                $available = -$amount;
                $desc = '销售员[' . $data['sale_name'] . ']提交订单,消费金额:' . $amount . '，订单号: ' . $data['order_id'];
                $this->getOrdersysCustomerModel()->newQuery()->where("id",$data["member_id"])->decrement("balance",$amount);
                break;
            case 'refund':  //退款
                $available = +$amount;
                $desc = '销售员[' . $data['sale_name'] . ']提交订单,被退款金额:' . $amount . '，订单号: ' . $data['order_id'];
                $this->getOrdersysCustomerModel()->newQuery()->where("id",$data["member_id"])->increment("balance",$amount);
                break;

        }
        $insertData = array(
            'member_id'=>$data["member_id"],
            'member_name'=>$data["member_name"],
            'type'=>$type,
            'amount'=>$available,
            'sale_uid'=>$data["sale_uid"],
            'sale_name'=>$data["sale_name"],
            'apply_uid'=>$data["apply_uid"],
            'apply_name'=>$data["apply_name"],
            'description'=>$desc,
            'remark'=>empty($data['remark'])?'':$data['remark'],
            'create_time'=>time(),
            'update_time'=>time(),
        );


        return $this->newQuery()->insert($insertData);

    }

}
